[在线等答案]ASP.NET中做 javascript 页面跳转

来源:百度知道 编辑:UC知道 时间:2024/06/19 17:36:36
我在用ASP.NET做网站,除了首页的登录页面外,如果直接访问其它页面,我希望用javascript里的confirm提示跳转,该怎么写?

(点确定就跳转,点取消就关闭IE)

if (Session["UserId"]==null)
{
Response.Write("<script language='javascript'>if(confirm('您无权访问本页面,请先登录。') document.location.replace('~/login.aspx');</script>");
}

这段代码不行。。。
不行。。。还没弹出提示就直接跳转了。。。

我要的效果是,点确定就跳到登录,点取消就关闭IE。

或者用alert也行,反正要给个提示再跳转。

if (Session["UserId"]==null)
{
Response.Write("<script language='javascript'>if(confirm('您无权访问本页面,请先登录。');location.href='/login.aspx';</script>");
}

这里为什么用confirm,用confirm,如果选择否的话,就会留在此页会不跳转,你的目的是什么?
应该是alert吧

protected void Button1_Click(object sender, EventArgs e)
{
if (Session["UserId"] == null)
{
string html = "<script language='javascript'>if(confirm('您无权访问本页面,请先登录。')) "+
" document.location.replace('"+GetHttpRoot(this.Request)+"/login.aspx');</script>";
this.Response.Write(html);
}
}

public string GetHttpRoot(HttpRequest request)
{
int index = request.Url.AbsoluteUri.IndexOf(request.Url.AbsolutePath);
string root